package com.songlanyun.modules.studyingDetail.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.modules.studying.service.StudyingService;
import com.songlanyun.modules.studyingDetail.dao.StudyingDetailDao;
import com.songlanyun.modules.studyingDetail.entity.StudyingDetail;
import com.songlanyun.modules.studyingDetail.service.StudyingDetailService;
import com.songlanyun.modules.studyingLately.dao.StudyingLatelyDao;
import com.songlanyun.modules.studyingLately.entity.StudyingLately;
import com.songlanyun.modules.studyingType.entity.StudyingType;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("studyingDetailService")
public class StudyingDetailServiceImpl extends ServiceImpl<StudyingDetailDao, StudyingDetail> implements StudyingDetailService {


    @Autowired
    private StudyingService studyingService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Long studying = MapUtil.getLong(params, "studying");
        String studyingDetailName = MapUtil.getStr(params, "studying_detail_name");
        Integer hasStatus = MapUtil.getInt(params, "has_status");

        IPage<StudyingDetail> page = this.page(
                new Query<StudyingDetail>().getPage(params),
                new LambdaQueryWrapper<StudyingDetail>()
                        .eq(StudyingDetail::getStudying, studying)
                        .like(StringUtils.isNotBlank(studyingDetailName), StudyingDetail::getStudyingDetailName, studyingDetailName)
                        .eq(ObjectUtil.isNotNull(hasStatus), StudyingDetail::getHasStatus, hasStatus)
                        .orderByAsc(StudyingDetail::getHasOrder)
                        .orderByDesc(StudyingDetail::getCreateTime));

        return new PageUtils(page);
    }

    @Override
    public StudyingDetail loadById(Long id, boolean b) {
        StudyingDetail studyingDetail = this.baseMapper.selectById(id);
        if (ObjectUtil.isNull(studyingDetail) && b) {
            throw new RRException("未找到该章节");
        }
        return studyingDetail;
    }

    @Override
    public StudyingDetail add(StudyingDetail studyingDetail) {
        //看课程合不合法
        studyingService.loadById(studyingDetail.getStudying(), true);
        loadByName(studyingDetail);
        this.baseMapper.insert(studyingDetail);
        return studyingDetail;
    }

    @Override
    public StudyingDetail update(StudyingDetail studyingDetail) {
        //看课程合不合法
        studyingService.loadById(studyingDetail.getStudying(), true);
        loadByName(studyingDetail);
        this.baseMapper.updateById(studyingDetail);
        return studyingDetail;
    }

    @Override
    public List<StudyingDetail> loadByStudyingId(Long studyingId) {
        return this.baseMapper.selectList(
                new LambdaQueryWrapper<StudyingDetail>()
                        .eq(StudyingDetail::getStudying, studyingId)
                        .eq(StudyingDetail::getHasStatus, true)
                        .orderByAsc(StudyingDetail::getHasOrder)
                        .orderByDesc(StudyingDetail::getCreateTime)
        );
    }

    @Override
    public void addVideoClick(Long id, int clicks) {
        this.baseMapper.addVideoClick(id, clicks);
    }

    private void loadByName(StudyingDetail studyingDetail) {
        Integer integer = this.baseMapper.selectCount(new LambdaQueryWrapper<StudyingDetail>()
                .eq(StudyingDetail::getStudying, studyingDetail.getStudying())
                .eq(StudyingDetail::getStudyingDetailName, studyingDetail.getStudyingDetailName())
                .ne(ObjectUtil.isNotNull(studyingDetail.getId()), StudyingDetail::getId, studyingDetail.getId()));
        if (integer > 0) {
            throw new RRException("该课程已有该章节,换个名字");
        }
    }
}
